package com.example.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.back.common.Result;
import com.example.back.entity.*;
import com.example.back.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.util.Length;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jm
 * @date 2024/7/19
 */
@RestController
@RequestMapping("/evaluation")
public class EvaluationController
{
    @Autowired
    private EvaluationService evaluationService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private ExpertRecordService expertRecordService;

    @Autowired
    private ExpertGroupService expertGroupService;

    @Autowired
    private SchoolGroupService schoolGroupService;

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public Result list()
    {
        return Result.success(evaluationService.list());
    }

    @PostMapping("/add")
    public Result addEvaluation(@RequestBody Evaluation evaluation)
    {
        evaluationService.saveOrUpdate(evaluation);
        return Result.success();
    }

    @GetMapping("/getActive")
    public Result selectActive()
    {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<Evaluation>();
        queryWrapper.eq("status", 1);
        List<Evaluation> evaluationList = evaluationService.list(queryWrapper);
        for(Evaluation evaluation : evaluationList)
        {
            evaluation.setTemplateName(templateService.getById(evaluation.getTemplateId()).getName());
        }
        return Result.success(evaluationList);
    }

    @GetMapping("/getList")
    public Result selectList()
    {
        List<Evaluation> evaluationList = evaluationService.list();
        for(Evaluation evaluation : evaluationList)
        {
            evaluation.setTemplateName(templateService.getById(evaluation.getTemplateId()).getName());
        }
        return Result.success(evaluationList);
    }


    @PostMapping("/updateRecord")
    public Result update(@RequestBody SelfRecord selfRecord)
    {
        QueryWrapper<SelfRecord> queryWrapper = new QueryWrapper<SelfRecord>();
        queryWrapper.eq("criterion_id", selfRecord.getCriterionId());
        queryWrapper.eq("school_id", selfRecord.getSchoolId());
        SelfRecord dbSelfRecord = recordService.getOne(queryWrapper);
        if(dbSelfRecord == null)
        {
            recordService.save(selfRecord);
        } else
        {
            selfRecord.setId(dbSelfRecord.getId());
            recordService.saveOrUpdate(selfRecord);
        }
        return Result.success();
    }


    @PostMapping("/updateExpertRecord")
    public Result update(@RequestBody ExpertRecord expertRecord)
    {
        QueryWrapper<ExpertRecord> queryWrapper = new QueryWrapper<ExpertRecord>();
        queryWrapper.eq("criterion_id", expertRecord.getCriterionId());
        queryWrapper.eq("school_id", expertRecord.getSchoolId());
        ExpertRecord dbExpertRecord = expertRecordService.getOne(queryWrapper);
        if(dbExpertRecord == null)
        {
            expertRecordService.save(expertRecord);
        } else
        {
            expertRecord.setId(dbExpertRecord.getId());
            expertRecordService.saveOrUpdate(expertRecord);
        }
        return Result.success();
    }


    @GetMapping("/finish/{id}")
    public Result evaluateFinish(@PathVariable Integer id)
    {
        Evaluation evaluation = evaluationService.getById(id);
        evaluation.setStatus(2);
        evaluationService.saveOrUpdate(evaluation);
        return Result.success();
    }


    @GetMapping("/schoolFinish")
    public Result schoolFinish(@RequestParam Integer schoolId,
                               @RequestParam Integer expertId)
    {
        QueryWrapper<ExpertGroup> expertGroupQueryWrapper = new QueryWrapper<ExpertGroup>();
        expertGroupQueryWrapper.eq("expert_id", expertId);
        ExpertGroup group = expertGroupService.getOne(expertGroupQueryWrapper);

        QueryWrapper<SchoolGroup> queryWrapper = new QueryWrapper<SchoolGroup>();
        queryWrapper.eq("user_id", schoolId);
        queryWrapper.eq("group_id", group.getId());
        SchoolGroup schoolGroup = schoolGroupService.getOne(queryWrapper);
        schoolGroup.setStatus(1);
        schoolGroupService.saveOrUpdate(schoolGroup);
        return Result.success();
    }


    @GetMapping("/getStatus")
    public Result getStatus()
    {
        Map<String, Integer> result = new HashMap<>();
        QueryWrapper<User> queryWrapperSchoolG = new QueryWrapper<User>();
        queryWrapperSchoolG.eq("identity", 3);
        List<User> gshool = userService.list(queryWrapperSchoolG);
        List<Integer> gids = new ArrayList<Integer>();
        for(User user : gshool)
        {
            gids.add(user.getId());
        }
        result.put("gCount", gshool.size());

        QueryWrapper<User> queryWrapperSchoolZ = new QueryWrapper<User>();
        queryWrapperSchoolG.eq("identity", 4);
        List<User> zshool = userService.list(queryWrapperSchoolZ);
        List<Integer> zids = new ArrayList<Integer>();
        for(User user : zshool)
        {
            zids.add(user.getId());
        }
        result.put("zCount", zshool.size());
        int gfinish = 0;
        int zfinish = 0;

        QueryWrapper<SchoolGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<SchoolGroup> schoolGroups = schoolGroupService.list(queryWrapper);
        for(SchoolGroup schoolGroup: schoolGroups)
        {
            if(zids.contains(schoolGroup.getUserId()))
            {
                zfinish += 1;
            }else
            {
                gfinish += 1;
            }
        }

        result.put("zfinish", zfinish);
        result.put("gfinish", gfinish);
        return Result.success(result);
    }
}
